package com.lian.chatroom.server.handler;

import com.lian.chatroom.message.GroupQuitRequestMessage;
import com.lian.chatroom.message.GroupQuitResponseMessage;
import com.lian.chatroom.server.session.Group;
import com.lian.chatroom.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 退出群聊，移除组成员
 * @ChannelHandler.Sharable 目的为了记录同时在线的设备数量
 */
@ChannelHandler.Sharable
public class GroupQuitRequestMessageHandler extends SimpleChannelInboundHandler<GroupQuitRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupQuitRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();
        String username = msg.getUsername();
        Group group = GroupSessionFactory.getGroupSession().removeMember(groupName, username);
        if (group != null){
            ctx.writeAndFlush(new GroupQuitResponseMessage(true,"已退出群" + groupName));
        }else {
            ctx.writeAndFlush(new GroupQuitResponseMessage(true,"群不存在" + groupName));
        }
    }
}
